package com.nd.android.u.ui.beanStarApp;

import ims.IMSdkEntry;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Notification;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;

import com.nd.android.u.allCommonUtils.FormatUtils;
import com.nd.android.u.allCommonUtils.PackageUtils;
import com.nd.android.u.chatInterfaceImpl.ChatInterfaceImpl;
import com.nd.android.u.commonWidget.NotificationMsg;
import com.nd.android.u.controller.ChatConst.KEY;
import com.nd.android.u.controller.ChatGlobalVariable;
import com.nd.android.u.controller.bean.DefaultGroup;
import com.nd.android.u.controller.bean.message.BaseDisplayMessage_System;
import com.nd.android.u.ui.R;

/**
 * 系统消息展示类，有些UI相关的操作要在这里进行
 * <br>Created 2014-10-30 上午11:05:54
 * @version  
 * @author   cb		
 * @see 	 
 */
public class DisplayMessage_System extends BaseDisplayMessage_System{

	
	/**
	 * DisplayMessage_System.构造方法
	 * <br>Created 2014-10-30 下午2:13:27
	 * @param groupKey
	 * @param groupType
	 */
	public DisplayMessage_System(String groupKey, int groupType) {
		super(groupKey, groupType);
	}
	
	@Override
    public void showNotify() {
    	if (ChatGlobalVariable.getInstance().noNeedNotify(this)){
            //不需要显示通知
            return;     
        }
        Context context = IMSdkEntry.INSTANCE.context;
        String flashText = context.getString(R.string.notify_system);
        // 设置剩余多少条未读消息
        String title = getUnreadMessage();

        // 设置点击后跳转的目标
        Intent notifyIntent = new Intent(context, getDisplayClass());
        Bundle bundle = new Bundle();
        bundle.putString(KEY.NAME, flashText);
        notifyIntent.putExtras(bundle);
        // 生成通知
        Notification notification = NotificationMsg.getInstance().obtainNotify(flashText, title, 
        		displayContent, notifyIntent);

        // 显示通知
        NotificationMsg.getInstance().showNotify(notification, this, false);
    }

    @Override
    public boolean prepareMessage() {
        if (displayContent != null) {
            return false;
        }
        try {
            // 申请，邀请，通过，拒绝群消息解析：是否为申请或者邀请群消息通过ToApply来决定
            // 通过，拒绝群消息有4种消息申请的拒绝和通过，邀请的拒绝和通过：通过approvalType来决定是申请还是邀请
            // 通过JOIN_GROUP_ACCEPTED来决定是通过还是拒绝
            JSONObject object = new JSONObject(oriMessage);
            // String approvalStr = object.getString("approvalStr");
            int approvalType = object.getInt("approvalType");
            int approvalResult = object.getInt("approvalResult");
            boolean ToApply = false;
            ToApply = object.getBoolean("ToApply");
            Resources resources = IMSdkEntry.INSTANCE.context.getResources();
            long fid = getFriendId();
            String applierName = ChatInterfaceImpl.INSTANCE.chatCallOtherModel.getUserName(fid);
            long gid = FormatUtils.parseLong(groupId);
            String groupName = groupId;
            
            if (approvalType == JOIN_GROUP_INVITE_OTHER) {
                // 你的好友 XXX 已同意、拒绝加入群 XXXXXX
                // 这部分代码现在应该没有用了，因为群主把人拉进群时不会去请求对方同意，那也就不用这个消息了。
                // 但是在Wiki
                // http://192.168.19.197/91uwiki/index.php?title=91U_IM%E6%B6%88%E6%81%AF%E6%A0%BC%E5%BC%8F
                // 上还有定义，所以留下这部分代码，以防万一。
                // 2014-10-11 11:41:07 陈友平
                if (approvalResult == JOIN_GROUP_ACCEPTED) {
                    groupName = ChatInterfaceImpl.INSTANCE.chatCallOtherModel.getGroupNameByGUid(
                            gid, ChatInterfaceImpl.INSTANCE.chatCallOtherModel.getOapUid());
                    oriMessage = String.format(
                            resources.getString(R.string.your_friend_agree_joined_group),
                            applierName, fid, groupName, gid);
                } else {
                    groupName = ChatInterfaceImpl.INSTANCE.chatCallOtherModel
                            .getLocalGroupName(gid);
                    oriMessage = String.format(
                            resources.getString(R.string.your_friend_reject_joined_group),
                            applierName, fid, groupName, gid);
                }
            } else if (approvalType == JOIN_GROUP_JOINTO_OTHER) {
                if (ToApply) {
                    groupName = ChatInterfaceImpl.INSTANCE.chatCallOtherModel.getGroupNameByGUid(gid,
                            ChatInterfaceImpl.INSTANCE.chatCallOtherModel.getOapUid());
                    if (approvalType != APPLY_TO_JOIN_GROUP) {
                        // 你的好友 XXX 申请加入群 XXX
                        oriMessage = String.format(
                                resources.getString(R.string.your_friend_request_to_join_group),
                                applierName, fid, groupName, gid);
                    } else {
                        // 你的好友 XXX 邀请您加入群  XXX
                        oriMessage = String.format(
                                resources.getString(R.string.your_friend_ask_u_to_join_group),
                                applierName, fid, groupName, gid);
                    }
                } else {
                    // XXXXX 管理人员 XXX 已通过、拒绝你加群的请求
                    if (approvalResult == JOIN_GROUP_ACCEPTED) {
                        groupName = ChatInterfaceImpl.INSTANCE.chatCallOtherModel
                                .getGroupNameByGUid(gid,
                                        ChatInterfaceImpl.INSTANCE.chatCallOtherModel.getOapUid());
                        oriMessage = String.format(
                                resources.getString(R.string.manager_agree_you_joined_group),
                                groupName, gid, applierName, fid);
                        // 登录群
                        new DefaultGroup(groupId, groupType).loginInit();
                        // GroupLoginManager.getInstance().loginInitFixGroup(groupKey, groupType);
                    } else {
                        groupName = ChatInterfaceImpl.INSTANCE.chatCallOtherModel
                                .getLocalGroupName(gid);
                        oriMessage = String.format(
                                resources.getString(R.string.manager_reject_you_joined_group),
                                groupName, gid, applierName, fid);
                    }
                }
            }

            displayContent = oriMessage;
        } catch (JSONException e) {
            ChatInterfaceImpl.INSTANCE.messageAnalyser.analyseMessage(this);
        }

        return false;
    }
	
    
    @Override
    public void onItemClick(Context context) {
//        AlertDialog.Builder dialog;
//        switch (messageContentType) {
//        case ServerMessageType.MSG_AUTH_ACCEPTED:
//        case ServerMessageType.MSG_ADDED:
//        case ServerMessageType.MSG_AUTH_REJECTED:
//        case ServerMessageType.GROUP_SYSMSG_TYPE_GROUP_DISMISSED:
//        case ServerMessageType.GROUP_SYSMSG_TYPE_USER_REMOVED:
//        case ServerMessageType.GROUP_SYSMSG_TYPE_AUTHORITY_ROLE_UPDATED:
//        case ServerMessageType.GROUP_SYSMSG_TYPE_APPROVED:
//            // 不做任何处理
//            break;
//        case ServerMessageType.MSG_AUTH_REQUEST:
//            if (!IMSStateManager.getInstance().isNetworkAvailable()) {
//                ToastUtils.display(R.string.net_warn_no_network);
//                return;
//            }
//            dialog = new AddFriendConfirmDialog(context, DisplayMessage_System.this);
//            dialog.create();
//            dialog.show();
//            break;
//        case ServerMessageType.GROUP_SYSMSG_TYPE_TO_APPROVE:
//            if (!IMSStateManager.getInstance().isNetworkAvailable()) {
//                ToastUtils.display(R.string.net_warn_no_network);
//                return;
//            }
//            Dialog dialog1 = new AddGroupConfirmDialog(context, DisplayMessage_System.this);
//            dialog1.show();
//            break;
//        default:
//            // 不做任何处理
//            break;
//        }
    }
}
